<template>
  <el-dialog
    :title="!dataForm.Id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="上级区域" prop="parentName">
        <el-popover ref="menuListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="id"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:menuListPopover
          :readonly="true"
          placeholder="点击选择上级区域"
          class="menu-list__input"
        ></el-input>
      </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input v-model="dataForm.orderNum" placeholder="显示顺序"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status">
          <el-option key="0" label="不可见" value="0"></el-option>
          <el-option key="1" label="可见" value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        Id: 0,
        parentId: "",
        name: "",
        orderNum: "",
        status: "",
        createTime: "",
        updateTime: "",
      },
      dataRule: {
        parentId: [
          { required: true, message: "父id不能为空", trigger: "blur" },
        ],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        orderNum: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "状态（0正常 1停用）不能为空",
            trigger: "blur",
          },
        ],
      },
      menuList: [],
      menuListTreeProps: {
        label: "name",
        children: "children",
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.Id = id || 0;
      this.visible = true;

      this.$http({
        url: this.$http.adornUrl("/tiku/paperku/list"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.menuList = treeDataTranslate(data.page, "id", "parentId");
        this.menuListTreeSetCurrentNode();
      });
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.Id) {
          this.$http({
            url: this.$http.adornUrl(`/tiku/paperku/info/${this.dataForm.Id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.parentId = data.paperKu.parentId;
              this.dataForm.name = data.paperKu.name;
              this.dataForm.orderNum = data.paperKu.orderNum;
              this.dataForm.status = data.paperKu.status + "";
            }
          });
        }
      });
    },

  // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      this.dataForm.pid = data.id;
      this.dataForm.parentName = data.name;
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode() {
      this.$refs.menuListTree.setCurrentKey(this.dataForm.pid);
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() ||
        {})["name"];
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/tiku/paperku/${!this.dataForm.Id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              Id: this.dataForm.Id || undefined,
              parentId: this.dataForm.parentId,
              name: this.dataForm.name,
              orderNum: this.dataForm.orderNum,
              status: this.dataForm.status,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
